﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Road3App.Model;
using Road3App.Model.CrossroadNs;
using Road3App.Model.RoadNs;
using Road3App.ViewModels;
using Road3App.Views.UserControls;
using Road3App.Views.UserControls.EditControls;

namespace Road3App.Views
{
    /// <summary>
    /// Interaction logic for MainView.xaml
    /// </summary>
    public partial class MainView : Window
    {
        private MainViewModel _viewModel;
        private bool _connMode;
        
        public MainView()
        {
            InitializeComponent();            

            _viewModel = new MainViewModel();
            this.DataContext = _viewModel;

            NewMI.Click += (s, ea) => _viewModel.NewRoadNetwork();
            OpenMI.Click += (s, ea) => _viewModel.LoadNetwork();
            SaveMI.Click += (s, ea) => _viewModel.SaveNetwork();
            SaveAsMI.Click += (s, ea) => _viewModel.SaveNetworkAs();

            ExitMI.Click += (s, ea) => Application.Current.Shutdown();

            AboutMI.Click += (s, ea) => new AboutWindow().ShowDialog();

            AddRoadButton.Click += (s, ea) =>
                {
                    Road road = new Road { Lanes = 1, Length = 100, SpeedLimit = 20 };
                    EditRoadUC eruc = new EditRoadUC(road);
                    EditWindow ew = new EditWindow(eruc);

                    if ((bool)ew.ShowDialog())
                        _viewModel.RoadNetwork.AddRoad(road);
                };

            AddCrossroadButton.Click += (s, ea) =>
                {
                    CrossRoad cr = new CrossRoad();
                    EditCrossRoadUC ecruc = new EditCrossRoadUC(cr);
                    EditWindow ew = new EditWindow(ecruc);

                    if ((bool)ew.ShowDialog())
                    {
                        cr.SwitchPhase();
                        cr.UpdateTrafficLightStates();
                        _viewModel.RoadNetwork.AddCrossRoad(cr);
                    }
                };

            AddVehGenButton.Click += (s, ea) =>
                {
                    VehicleGenerator vg = new VehicleGenerator();
                    EditVehGen evg = new EditVehGen(vg);
                    EditWindow ew = new EditWindow(evg);

                    if ((bool)ew.ShowDialog())
                        _viewModel.RoadNetwork.AddVehGen(vg);
                };

            AddConnectorButton.Click += (s, ea) =>
                {
                    this.AllowDrop = _connMode = !_connMode;

                    if (_connMode)
                    {
                        _viewModel.ContainerCollection.ToList().ForEach(tc =>
                        {
                            tc.MouseLeftButtonDown += tc_MouseLeftButtonDown;
                            tc.Drop += tc_Drop;
                        });
                    }
                    else
                    {
                        _viewModel.ContainerCollection.ToList().ForEach(tc =>
                        {
                            tc.MouseLeftButtonDown -= tc_MouseLeftButtonDown;
                            tc.Drop -= tc_Drop;
                        });
                    }
                    
                };

            InitButton.Click += (s, ea) => _viewModel.InitTest();
            StartStopButton.Click += (s, ea) => _viewModel.StartStop();            
        }

        #region Drag N Drop

        void tc_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TransformControl tc = sender as TransformControl;
            if (tc == null)
                return;

            DataObject dragData = new DataObject("TransformControl", tc);
            DragDrop.DoDragDrop(tc, dragData, DragDropEffects.Move);
        }

        void tc_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("TransformControl"))
            {
                TransformControl otherTc = e.Data.GetData("TransformControl") as TransformControl;
                TransformControl tc = sender as TransformControl;

                Container first = otherTc.DataContext as Container;
                Container second = tc.DataContext as Container;

                if (first != second)
                    _viewModel.ConnectContainers(first, second);

            }
        }

        #endregion
    }
}
